package com.kgc.util;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;



public class fileUtil {
    public static String uploadFile(ServletContext application, MultipartFile photoFile){
        String rootPath = application.getRealPath("/");
        System.out.println(rootPath);
        String dirName = "uploadImg";
        String dirPath = rootPath + dirName;
        File dirFile = new File(dirPath);
        if(!dirFile.exists()){
            dirFile.mkdir();
        }

        String fileName = photoFile.getOriginalFilename();

        String uuid = UUIDUtil.createUUID();
        String newName = uuid + fileName.substring(fileName.lastIndexOf("."));

        File file = new File(dirPath + "/" + newName);

        try {
            photoFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return dirPath + "/" + newName;
    }
}
